﻿using NetCore.Entities.Projects;
using NetCore.Interfaces;
using NetCore.Projects.Dtos;
using NetCore.Services;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace NetCore.Projects
{
    /// <summary>
    /// 项目用户
    /// </summary>
    [Description("项目用户")]
    public class ProjectUserAppService : BaseNetCoreAppService<ProjectUser>, IProjectUserAppService
    {
        private INetCoreRepository<Project> _projectRepository;
        public ProjectUserAppService(
            INetCoreRepository<Project> projectRepository,
            INetCoreRepository<ProjectUser> baseRepository)
            : base(baseRepository)
        {
            _projectRepository = projectRepository;
        }
        /// <summary>
        /// 添加项目用户
        /// </summary>
        /// <param name="input">projectId userIds</param>
        public async Task AddProjectUsers(AddProjectUserInputDto input)
        {
            if (input.ProjectId.HasValue)
            {
                var project = _projectRepository.Get(input.ProjectId.Value);
                var projectUsers = input.UserIds.Select(userId => new ProjectUser
                {
                    ProjectId = input.ProjectId,
                    UserId = userId,
                    OrganizationId = project?.OrganizationId
                }).ToList();
                await BaseRepository.BulkInsertAsync(projectUsers);
            }
        }
    }
}
